<?php

/**
 * Maikuro Framework
 *
 * LICENSE:
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * The license is bundled with this package in the file LICENSE.txt.
 * You may also obtain a copy of the License at this URL:
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @copyright  Copyright (c) 2010, Jonathan Hedrén
 * @author     Jonathan Hedrén <jonathan.hedren@gmail.com>
 * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
 */

/**
 * 
 */
class Filters
{
    /**
     * HTML escape filter.
     *
     * @param string $value
     * @return string
     */
    public static function htmlEscape($value)
    {
        return filter_var($value, FILTER_SANITIZE_SPECIAL_CHARS);
    }

   /**
     * Integer filter.
     *
     * @param mixed $value
     * @return integer
     */
    public static function int($value)
    {
        return filter_var($value, FILTER_SANITIZE_NUMBER_INT);
    }

    /**
     * Strip HTML tags filter.
     *
     * @param string $value
     * @return string
     */
    public static function stripTags($value)
    {
        return filter_var($value, FILTER_SANITIZE_STRING);
    }
    
    /**
     * Trim filter
     *
     * @param string $value
     * @return string
     */
    public static function trim($value)
    {
        return trim($value);
    }

    /**
     * URL encode filter.
     *
     * @param string $value
     * @return string
     */
    public static function urlEncode($value)
    {
        return filter_var($value, FILTER_SANITIZE_ENCODED);
    }

    /**
     * Email filter.
     *
     * @param string $value
     * @return string
     */
    public static function email($value)
    {
        return filter_var($value, FILTER_SANITIZE_EMAIL);
    }

    /**
     * URL filter.
     *
     * @param string $value
     * @return string
     */
    public static function url($value)
    {
        return filter_var($value, FILTER_SANITIZE_URL);
    }
	
     /**
     * Converts a string to a URL friendly string by replacing umlauts etc with
     * their closest 7-bit ASCII equivalent.
     *
     * @param string $string
     * @param string $separator
     * @param boolean $lowerCaseOnly
     * @return string
     */
    public static function urlFriendly($string, $separator = '-', $lowerCaseOnly = true) {
        $replacementMap = array(
            'À' => 'A',
            'Á' => 'A',
            'Â' => 'A',
            'Ã' => 'A',
            'Ä' => 'A',
            'Å' => 'A',
            'Æ' => 'AE',
            'Ç' => 'C',
            'Č' => 'C',
            'Ć' => 'C',
            'Ð' => 'Dj',
            'È' => 'E',
            'É' => 'E',
            'Ê' => 'E',
            'Ë' => 'E',
            'Ì' => 'I',
            'Í' => 'I',
            'Î' => 'I',
            'Ï' => 'I',
            'Ñ' => 'N',
            'Œ' => 'OE',
            'Ò' => 'O',
            'Ó' => 'O',
            'Ô' => 'O',
            'Õ' => 'O',
            'Ö' => 'O',
            'Ø' => 'O',
            'Ŕ' => 'R',
            'Š' => 'S',
            'Ù' => 'U',
            'Ú' => 'U',
            'Û' => 'U',
            'Ü' => 'U',
            'Ÿ' => 'Y',
            'Ý' => 'Y',
            'Ž' => 'Z',
            'Þ' => 'B',
            'ß' => 'ss',
            'à' => 'a',
            'á' => 'a',
            'â' => 'a',
            'ã' => 'a',
            'ä' => 'a',
            'å' => 'a',
            'æ' => 'ae',
            'þ' => 'b',
            'ç' => 'c',
            'č' => 'c',
            'ć' => 'c',
            'đ' => 'dj',
            'è' => 'e',
            'é' => 'e',
            'ê' => 'e',
            'ë' => 'e',
            'ì' => 'i',
            'í' => 'i',
            'î' => 'i',
            'ï' => 'i',
            'ð' => 'o',
            'ñ' => 'n',
            'œ' => 'oe',
            'ò' => 'o',
            'ó' => 'o',
            'ô' => 'o',
            'õ' => 'o',
            'ö' => 'o',
            'ø' => 'o',
            'ŕ' => 'r',
            'š' => 's',
            'ù' => 'u',
            'ú' => 'u',
            'û' => 'u',
            'ü' => 'u',
            'ý' => 'y',
            'ý' => 'y',
            'ÿ' => 'y',
            'ž' => 'z'
        );

        if ($lowerCaseOnly) {
            $string = strtolower($string);
        }

        $urlFriendly = strtr($string, $replacementMap);
        $urlFriendly = preg_replace('/[^a-z0-9_-]/i', $separator, $urlFriendly);
        $urlFriendly = preg_replace('/' . $separator . '+/', $separator, $urlFriendly);
        $urlFriendly = trim($urlFriendly, ' ' . $separator);

        return $urlFriendly;
    }
}
